package com.ymatou.iapi.csim.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.csim.parameter.CheckAvailableBean;
import com.ymatou.iapi.csim.parameter.SendMessageCSBean;
import com.ymatou.iapi.csim.service.CheckAvailableCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.model.ymt_onlinecs;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_queuesessionWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
/**
*获取所有客服
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CheckAvailable{
	private static CheckAvailableBean bean;
	private static CheckAvailableCall call;
	private CsimCallService service=new CsimCallService();
	private Integer csId=Integer.parseInt(EnvSetup.getData("csId").toString());
	private static Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private static ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
	private static ymt_onlinecsWapper onlinecsWapper=new ymt_onlinecsWapper();
	private static ymt_queuesessionWapper queuesessionWapper=new ymt_queuesessionWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("客服是否可以对某一用户发起会话");
	}

	@Before
	public void caseUp() {
		bean=new CheckAvailableBean();
		call=new CheckAvailableCall();
		Logger.comment("删除历史数据");
		onlinecsWapper.deleteBy(MapUtil.hashMap("csId",csId,"Status",0));
		csSessionWapper.deleteByUserId(MapUtil.hashMap("UserId",userId,"Status",0));
		queuesessionWapper.deleteAll();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckAvailable_1() {
		Logger.start(true,"客服是否可以对某一用户发起会话-验证可以发起会话");
		try {
			//客服上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csId);
			bean.setCsId(csId);
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();		
			//验证
			Logger.verifyEquals("0", call.getResult().get("ErrorCode").toString(), "验证errorCode");
			Logger.verifyEquals(true, call.getResult().get("Success"), "验证Success");
			Logger.verifyEquals(true, call.getResult().get("Available"), "验证Available 是否可以发起会话 ");
			Logger.verifyEquals("", call.getResult().get("CSName"), "验证CSName ");//avaliable=false才有返回
			Logger.verifyEquals("200", call.getStatus(), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckAvailable_2() {
		Logger.start(true,"客服是否可以对某一用户发起会话-验证当前客服量=最大用户量，可发起会话(主动发起不受最大接线量限制)");
		try {
			//客服上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csId);
			onlinecs.setStatus(0);
			onlinecs.setCurrentSessions(onlinecs.getMaxSessions());
			onlinecsWapper.updateByPrimaryKey(onlinecs);
			
			bean.setCsId(csId);
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();		
			//验证
			Logger.verifyEquals("0", call.getResult().get("ErrorCode").toString(), "验证errorCode");
			Logger.verifyEquals(true, call.getResult().get("Success"), "验证Success");
			Logger.verifyEquals(true, call.getResult().get("Available"), "验证Available 是否可以发起会话 ");
			Logger.verifyEquals("", call.getResult().get("CSName"), "验证CSName ");//avaliable=false才有返回
			Logger.verifyEquals("200", call.getStatus(), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckAvailable_3() {
		Logger.start(true,"客服是否可以对某一用户发起会话-验证会话已存在，不可发起会话");
		try {
			//客服上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csId);
			//打开会话
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			//发消息
			SendMessageCSBean sendMessageCSBean=service.setSendMessageCSBean();
			sendMessageCSBean.setSessionId(cssession.getId());
			sendMessageCSBean.setCsId(csId);
			sendMessageCSBean.setUserId(userId);
			service.setSendMessageCSCall(sendMessageCSBean);
			
			bean.setCsId(csId);
			bean.setUserId(userId);
			call.setData(bean);
			call.callService();		
			//验证
			Logger.verifyEquals("0", call.getResult().get("ErrorCode").toString(), "验证errorCode");
			Logger.verifyEquals(true, call.getResult().get("Success"), "验证Success");
			Logger.verifyEquals(false, call.getResult().get("Available"), "验证Available 是否可以发起会话 ");
			Logger.verifyEquals(onlinecs.getCSName(), call.getResult().get("CSName"), "验证CSName ");//avaliable=false才有返回
			Logger.verifyEquals("200", call.getStatus(), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}